package main

import (
	"fmt"
	"github.com/go-ini/ini"
	"github.com/sirupsen/logrus"
	"logagent/algorithm/circlesinglelink"
	"logagent/etcd"
	"logagent/kafka"
	"logagent/tailfile"
	"os"
	"strings"
)

var (
	log *logrus.Logger
)

type KafkaConfig struct {
	Address  string `ini:"address"`
	ChanSize int    `ini:"chan_size"`
}

type CollectConfig struct {
	LogFilePath string `ini:"logfile_path"`
}

type EtcdConfig struct {
	Address    string `ini:"address"`
	CollectKey string `ini:"collect_key"`
}

// 整个logagent的配置结构体
type Config struct {
	KafkaConfig   `ini:"kafka"`
	CollectConfig `ini:"collect"`
	EtcdConfig    `ini:"etcd"`
}

func initLogger() {
	log = logrus.New()
	// 设置日志输出为os.Stdout
	log.Out = os.Stdout
	log.Level = logrus.DebugLevel
	// 可以设置像文件等任意`io.Writer`类型作为日志输出
	// file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	// if err == nil {
	//  log.Out = file
	// } else {
	//  log.Info("Failed to log to file, using default stderr")
	// }
	log.Info("init log success")
}

/**
func run() (err error) {
		// TailObj --> log --> Client --> kafka
		for {
			// 循环读数据
			line, ok := <-tailfile.TailObj.Lines // chan tail.Line
			if !ok {
				logrus.Warn("tail file close reopen,filename:%s\n", tailfile.TailObj.Filename)
				time.Sleep(time.Second)
				continue
			}
			// 如果是空行就略过
			if len(strings.Trim(line.Text, "\r")) == 0 {
				logrus.Info("出现空行啦，直接跳过....")
				continue
			}
			// 利用通道将同步的代码改成异步的
			// 把读出来的一行日志包装成kafka里面的msg类型
			msg := &sarama.ProducerMessage{}
			msg.Topic = "web_log"
			msg.Value = sarama.StringEncoder(line.Text)
			// 丢到通道中
			kafka.ToMsgChan(msg)
		}
	}
**/

func run() {
	select {}
}

func main2() {
	initLogger()
	var configObj = new(Config)
	// 1.初始化配置文件
	err := ini.MapTo(configObj, "./conf/config.ini")
	if err != nil {
		panic(fmt.Sprintf("init config failed err:%v", err))
	}
	// 2.初始化kafka
	kafka.Init(strings.Split(configObj.KafkaConfig.Address, ","), configObj.KafkaConfig.ChanSize)
	if err != nil {
		panic(fmt.Sprintf("init kafka failed,err:%v", err))
	}

	logrus.Info("init kafka success!")

	// 3、初始化etcd连接
	err = etcd.Init([]string{configObj.EtcdConfig.Address})
	if err != nil {
		logrus.Errorf("init etcd failed,err:%v", err)
	}

	// 4、从etcd中拉取要收集日志的配置项
	allConf, err := etcd.GetConf(configObj.EtcdConfig.CollectKey)
	if err != nil {
		logrus.Errorf("get conf from etcd failed,err:%v", err)
	}
	fmt.Println("---allConf---", allConf)

	// 5、派一个小弟去监控etcd中 configObj.EtcdConfig.CollectKey 对应值的变化
	go etcd.WatchConf(configObj.EtcdConfig.CollectKey)

	// 6.根据配置中的日志路径初始化tail
	err = tailfile.Init(allConf) // 把从etcd中获取的配置项传到Init
	if err != nil {
		logrus.Error("init tailfile failed,err:%v", err)
		return
	}
	logrus.Info("init tailfile success!")
	//
	//// 3.把日志通过sarama发往kafka
	//err = run()
	//if err != nil {
	//	logrus.Error("run failed,err :%v", err)
	//	return
	//}
	run()
}

func main() {
	//singlequeue.TestSingleQueue()
	//circlequeue.TestCircleQueue()
	circlesinglelink.TestCircleSingleLink()
	//josephu.TestJosephu()
}
